import datetime

class Event:
    def __init__(self, EID, EventName, StartDate, EndDate, EventDesc):
        self.EID = EID
        self.EventName = EventName
        self.StartDate = StartDate
        self.EndDate = EndDate
        self.EventDesc = EventDesc

def FormatTimeDateToString(DateTimeObj):
    STR = """%s, %s, %s"""%(str(DateTimeObj.year), str(DateTimeObj.month), str(DateTimeObj.day))
    return STR

def FormatStringtoTimeDate(String):
    Numbers = String.split(',')
    return datetime.date(int(Numbers[0]), int(Numbers[1]), int(Numbers[2]))
    
class Event_Data:
    def __init__(self, SQLConn, dBName = "Event_Data"):
        self.EventDataTableName = dBName
        self.SQLConn = SQLConn

    def AddEvent(self, EventName, StartDate, EndDate, Discription):
        self.SQLConn.SendCommand("""
         INSERT INTO %s (Event_name, Event_StartDate, Event_EndDate, Event_Discription)
         VALUES ('%s', '%s', '%s', '%s')
       """ % (self.EventDataTableName, EventName, FormatTimeDateToString(StartDate), FormatTimeDateToString(EndDate), Discription))
    
    def RemoveEvent(self, EID):
        self.SQLConn.SendCommand("""
         DELETE FROM %s where Eve_id=%d
         """ % (self.EventDataTableName, EID))
        
    def GetAllEvents(self):
        self.SQLConn.SendCommand("""SELECT Eve_id, Event_name, Event_StartDate, Event_EndDate, Event_Discription FROM %s"""%(self.EventDataTableName))
        rows = self.SQLConn.FetchAll ()
        Events = []
        for event in rows:
            Events.append(Event(int(event[0]), event[1], FormatStringtoTimeDate(event[2]), FormatStringtoTimeDate(event[3]), event[4]))
        return Events
    
    def GetAllEventsDict(self):
        self.SQLConn.SendCommand("""SELECT Eve_id, Event_name, Event_StartDate, Event_EndDate, Event_Discription FROM %s"""%(self.EventDataTableName))
        rows = self.SQLConn.FetchAll ()
        Events = {}
        for event in rows:
            Events[int(event[0])] = Event(int(event[0]), event[1], FormatStringtoTimeDate(event[2]), FormatStringtoTimeDate(event[3]), event[4])
        return Events
    
    def UpdateEvent(self, EID, EventName, StartDate, EndDate, Discription):
       self.SQLConn.SendCommand("""UPDATE %s
                                SET Event_name = '%s',
                                Event_StartDate = '%s',
                                Event_EndDate = '%s',
                                Event_Discription = '%s'
                                WHERE Eve_id = %d
                                """ % (self.EventDataTableName, EventName, FormatTimeDateToString(StartDate), FormatTimeDateToString(EndDate), Discription, EID))
       
    def GetEvent(self, EID=-1, EventName='', StartDate='', EndDate='', Discription=''):
        First = True
        str = """WHERE """
        if EventName != '':
            if First:
                First = False
                str = str + """Event_name = '%s' """ % EventName
            else:
                str = str + """AND Event_name = '%s' """ % EventName
        if EID != -1:
            if First:
                First = False
                str = str + """Eve_id = %d """ % EID
            else:
                str = str + """AND Eve_id = '%d' """ % EID
        if StartDate != '':
            if First:
                First = False
                str = str + """Event_StartDate = '%s' """ % FormatTimeDateToString(StartDate)
            else:
                str = str + """AND Event_StartDate = '%s' """ % FormatTimeDateToString(StartDate)
        if EndDate != '':
            if First:
                First = False
                str = str + """Event_EndDate = '%s' """ % FormatTimeDateToString(EndDate)
            else:
                str = str + """AND Event_EndDate = '%s' """ % FormatTimeDateToString(EndDate)
        if Discription != '':
            if First:
                First = False
                str = str + """Event_Discription = '%s' """ % Discription
            else:
                str = str + """AND Event_Discription = '%s' """ % Discription
        
        self.SQLConn.SendCommand("""SELECT Eve_id, Event_name, Event_StartDate, Event_EndDate, Event_Discription FROM %s """%(self.EventDataTableName) + str)
        Data = self.SQLConn.FetchAll ()
        if len(Data) >  1:
            Res = []
            for Eve in Data:
                Res.append(Event(int(Eve[0]), Eve[1], FormatStringtoTimeDate(Eve[2]), FormatStringtoTimeDate(Eve[3]), Eve[4]))
            
        elif len(Data) == 1:
            Eve = Data[0]
            Res = [Event(int(Eve[0]), Eve[1], FormatStringtoTimeDate(Eve[2]), FormatStringtoTimeDate(Eve[3]), Eve[4])]
        else:
            Res = None
            
        return Res


def GenerateTables(SQL):
    print """Generating "Event_Data" Table"""
    SQL.SendCommand ("DROP TABLE IF EXISTS Event_Data")
    SQL.SendCommand("""CREATE TABLE Event_Data(
                    Eve_id int unsigned not null auto_increment primary key,
                    Event_name varchar(20),
                    Event_StartDate BLOB,
                    Event_EndDate BLOB,
                    Event_Discription varchar(1000)
                    )
                    """)